from enum import Enum
from typing import Optional, Generic, TypeVar
from pydantic import BaseModel, Field


class Token(BaseModel):
    """ 返回给用户的token """
    access_token: str = Field(..., description='令牌')
    token_type: str = Field(default='Bearer', description='类型')


class CustomMessageType(str, Enum):
    normal = 'Normal'  # 正常
    validation = 'ValidationError'  # 请求值错误
    business = 'BusinessError'  # 业务错误
    server = 'ServerError'  # 服务错误


class CustomMessage(BaseModel):
    """
    存放响应消息，用作通讯
    """
    type: CustomMessageType = Field(None, description='类型', example="ValidationError")
    code: Optional[str] = Field(None, description='编码')
    desc: Optional[str] = Field(None, description='描述')


T = TypeVar("T")


class CustomResponse(Generic[T], BaseModel):
    """
    自定义的响应格式，包含代码、响应详情和数据。
    """
    code: str = Field(..., description='响应代码')
    message: CustomMessage = Field(..., description='响应雄安锡')
    data: Optional[T] = Field(None, description='响应数据')

    # class Config:
    #     schema_extra = {
    #         'example': {
    #             'code': '200',
    #             'message': {
    #                 'type': 'Normal',
    #                 'code': 'U001',
    #                 'desc': '一切正常'
    #             },
    #             'data': {"key": "value"}
    #         }
    #     }
